<?php
//模型
class UserSportPgModel extends MY_Model {
	function __construct(){
		parent::__construct();
		$this->table = 'user_sport_pg';
	}
	//分页列表
	function get_list($arr=array(),$page=1,$size=10){		
		$where = " where 1 ";
		if(isset($arr['user_id'])){
			$where .= " and p.user_id='{$arr['user_id']}' ";
		}
		if(isset($arr['sid'])){
			$where .= " and p.sid='{$arr['sid']}' ";
		}
		
		$sql  = "select p.id, p.position, s.name from 
				".$this->db->dbprefix.$this->table." as p left join 
				".$this->db->dbprefix."sport as s on p.sid=s.id
				{$where}
				LIMIT ".($page-1)*$size .",{$size}";
				
		$query = $this->db->query($sql);
		$res = $query->result_array();		
		
		$sql  = "select count(*) as num from ".$this->db->dbprefix.$this->table." as p ".$where;
		$query = $this->db->query($sql);
		$row = $query->row_array();		
		return array('list'=>$res,'count' =>$row['num']);		
	}
	//全部列表
	function get_lists($id,$user_id){		
		$where = " where 1 ";
		if(isset($user_id)){
			$where .= " and p.user_id='{$user_id}' ";
		}
		if(isset($id)){
			$where .= " and p.sid='{$id}' ";
		}
		//var_dump($where);exit;
		
		$sql  = "select p.id, p.position, p.info, p.img, s.name from 
			  ".$this->db->dbprefix.$this->table." as p left join 
		      ".$this->db->dbprefix."sport as s on p.sid=s.id ".$where;
			  
		$query = $this->db->query($sql);
		$res = $query->result_array();
		return $res;		
	}
	//详情
	function get_info($id){
		$sql  = "select p.id, p.position, p.info, p.img, s.name from ".$this->db->dbprefix.$this->table." as p left join ".$this->db->dbprefix."sport as s on p.sid=s.id where p.id='{$id}' ";
		$query = $this->db->query($sql);
		$row = $query->row_array();		
		return $row;
	}
	//唯一条件获取详情
	function unique_get_info($field,$value){
		$sql  = "select * from ".$this->db->dbprefix.$this->table." where {$field}='{$value}' ";
		$query = $this->db->query($sql);
		$row = $query->row_array();		
		return $row;
	}
	
}

